<?php
//代写服务
namespace Home\Controller;
use Think\Controller;
class WriteController extends CommonController {
	
    public function index(){		
   		$this->redirect("writeService");
    }
	//文案代写
    public function writeService(){

        //获取分类
        $writeType=M("write_type")->select();
        //获取套餐
        $typePackage=M("write_type_package")->select();

        $this->assign("writeType",$writeType);
        $this->assign("typePackage",$typePackage);

		$this->display();
    }
    //文案代写-提交表单
    public function writeServiceForm(){
       
       $map=I("post.");
        
        if(!$map){
            $this->error("","writeService");
            exit;
        }

        $writeModel=M("write");
        
        $data["type_id"]=$map["writeType"];
        $data["package_id"]=$map["typePackage"];
        $data["number"]=$map["number"];
        $data["title"]=$map["title"];
        $data["content"]=$map["content"];
        $data["uid"]=$_SESSION["user"]["id"];
        $data["commit_time"]=date("Y-m-d H:i:s");
        $data["money"]=$map["money"];
        
        //验证表单重复提交
        $autoCheckToken=$writeModel->autoCheckToken($map);
        if (!$autoCheckToken){
            $this->error("表单重复提交","writeService");
            exit;
        }

        $res=$writeModel->add($data);
        
        if($res!==false){
            $this->success("","myWrite");  
        }else{
            $this->error("","writeService");
        }
    }

    //文案代写详情
    public function writeDetails(){

        $id=I("get.id");
        if(!$id){
            $this->error("","writeService");
            exit;
        }

        $where="id=".$id;
        $write=M("write")->where($where)->find();

        //获取分类
        $writeTypeArr=M("write_type")->select();
        //获取套餐
        $typePackageArr=M("write_type_package")->select();        
        foreach ($writeTypeArr as $key => $value) {
            $writeType[$value["id"]]=$value["title"];
        }
        foreach ($typePackageArr as $key => $value) {
            $typePackage[$value["id"]]=$value["title"];
        }
        $this->assign("writeType",$writeType);
        $this->assign("typePackage",$typePackage);

        //格式化新闻内容
        $write["content"]=htmlspecialchars_decode($write["content"]);
        $this->assign("write",$write);

        $this->display();       
    }


	//我的代写
    public function myWrite(){

        $writeModel=M("write");
        
        $comList=getPages($writeModel);

        $this->assign("writeList",$comList["comList"]);
        $this->assign("page",$comList["page"]);
        
        $status=getWriteStatus();
        $this->assign("apply_status",$status["apply"]);
        $this->assign("ok_status",$status["ok"]);
        
        $this->display();
    }
}